package com.paomi.client.activity.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.activity.detail.model.DetailTicketBean;
import com.paomi.client.activity.detail.model.TipCommentBean;
import com.paomi.client.activity.message.model.MyGetReplyList;
import com.paomi.client.activity.reply.ReplyActivity;
import com.paomi.client.base.BaseLCEActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.UserUtils;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.Datas;
import com.paomi.client.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by pc on 2018/11/9
 * Describe:消息回复列表页面
 * com.paomi.client.activity.reply
 */
public class ReplyMessageActivity extends BaseLCEActivity<MyGetReplyList.MyReplyJson, ReplyMessagePresenter> {

    @BindView(R.id.reply_list)
    RecyclerView replyList;
    @BindView(R.id.button_goback)
    TextView buttonGoback;
    private int page = 1;//页码数
    private DetailTicketBean.TicketBean ticketBean = null;
    private List<MyGetReplyList.MyReplyJson> messageList = new ArrayList<>();
    private boolean isRead = false;
    private List<TipCommentBean> beanList;

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.msg_reply);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + "返回");
        button_goback.setTypeface(iconfont);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_reply_msg;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        buttonGoback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isRead) {
                    Intent intent = new Intent();
                    intent.putExtra("num", "1");
                    setResult(2003, intent);
                }
                finish();
            }
        });
        getPresenter().setReplyContent(page,this);
    }

    @NonNull
    @Override
    public ReplyMessagePresenter createPresenter() {
        return new ReplyMessagePresenter();
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return replyList;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_reply_list;
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_message_empty, 0, 0);
        empty.setText(getResources().getString(R.string.null_msg));
    }

    @Override
    protected void convert(BaseViewHolder helper, MyGetReplyList.MyReplyJson item) {
        helper.setVisible(R.id.ll_show, true);
        RoundedImageView iv_head = helper.itemView.findViewById(R.id.user_image);
        ImageView iv_show = helper.itemView.findViewById(R.id.iv_show);
        GlideApp.with(this).asBitmap().load(item.getUserImg()).placeholder(R.mipmap.img_default_head).into(iv_head);
        GlideApp.with(this).asBitmap().load(item.getShowImg()).placeholder(R.mipmap.img_default_goods).into(iv_show);
        helper.setText(R.id.comment_user, item.getUserName());
        helper.setText(R.id.comment_content, item.getContent());
        TextView praise_iv = helper.itemView.findViewById(R.id.praise_iv);
        TextView praise_num = helper.itemView.findViewById(R.id.praise_num);
        praise_iv.setText(getResources().getString(R.string.msg_comment_imag));
        praise_iv.setTypeface(iconfont);
        //时间
        helper.setText(R.id.comment_time, Datas.getTimeElapse(item.getTime()));
        helper.setText(R.id.tv_show, item.getShowName());
        String myName = "";
        if (UserUtils.getInstance().getUserInfo1() != null) {
            myName = "@" + UserUtils.getInstance().getUserInfo1().getUserName();
        }
        String ht = "<font color='#108EE9'>" + myName + "</font>" + ":" + item.getReplyContent();
        helper.setText(R.id.reply_txt, Html.fromHtml(ht));
        //点击方式
        //评论内容去回复页面
        helper.setOnClickListener(R.id.comment_content, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getType() == 0) {
                    Intent intent = new Intent(ReplyMessageActivity.this, ReplyActivity.class);
                    intent.putExtra("getId", "" + item.getTypeId());//商品id
                    intent.putExtra("pid", "" + item.getMainCommentId());//被评论的id
                    startActivity(intent);
                }
            }
        });
        //点击商品去详情
        helper.setOnClickListener(R.id.ll_show, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getType() == 0) {
                    isRead = true;
                    getPresenter().clickReaded("" + item.getId());
                    Intent intent = new Intent(ReplyMessageActivity.this, DetailTicketActivity.class);
                    intent.putExtra("getId", "" + item.getTypeId());
                    startActivity(intent);
                }
            }
        });
        //举报
        helper.setOnClickListener(R.id.comment_tip, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                beanList=new ArrayList<>();
                TipCommentBean tip1=new TipCommentBean();
                tip1.setValue(getResources().getString(R.string.tip_one));
                beanList.add(tip1);
                TipCommentBean tip2=new TipCommentBean();
                tip2.setValue(getResources().getString(R.string.tip_two));
                beanList.add(tip2);
                TipCommentBean tip3=new TipCommentBean();
                tip3.setValue(getResources().getString(R.string.tip_there));
                beanList.add(tip3);
                if (beanList != null) {
                    getPresenter().tipComment(ReplyMessageActivity.this, iconfont, beanList, "" + item.getId());
                }
            }
        });
        //回复
        helper.setOnClickListener(R.id.praise_iv, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getPresenter().showCommentDialog(ReplyMessageActivity.this, item);
            }
        });
    }

    @Override
    protected void onItemClick(View view, MyGetReplyList.MyReplyJson item) {
//        if (messageList.size() == 1) {//只有一条数据并且已读
//
//        }
        isRead = true;
        getPresenter().clickReaded("" + item.getId());
    }

    @Override
    public void setData(List<MyGetReplyList.MyReplyJson> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        messageList.clear();
        messageList.addAll(data);
        if (data.size() % Constants.PAGE_SIZE != 0) {//移除FooterView
            getmAdapterWrapper().setLoadItemVisibility(false);
        } else {
            getmAdapterWrapper().setLoadItemVisibility(true);
        }
        getmLoadMoreHelper().setLoadMoreFinish();
        getmAdapterWrapper().notifyDataSetChanged();
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe
    public void onEvent(Object result) {
        if (result instanceof BaseJSON) {
            if (((BaseJSON) result).returnMessage == null) {
                return;
            }
            if (((BaseJSON) result).returnMessage.equals("发布成功")) {
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.input_sure_msg));
            } else if (((BaseJSON) result).returnMessage.equals("举报成功")) {
                ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.tip_success));
            }
        }
    }

    @Override
    public void onLoad() {
        super.onLoad();
        if (messageList.size() % Constants.PAGE_SIZE == 0) {
            page++;
            getPresenter().setReplyContent(page,this);
        } else {
            getmAdapterWrapper().setLoadItemVisibility(false);
            getmLoadMoreHelper().setLoadMoreFinish();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(isRead) {
            Intent intent = new Intent();
            intent.putExtra("num", "1");
            setResult(2003, intent);
        }
    }
}
